package cn.edu.zjicm.wordsnet_d.activity;

import com.umeng.analytics.MobclickAgent;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.synchronize.ForgetPwdInterface;
import cn.edu.zjicm.wordsnet_d.synchronize.UserInterface;
import cn.edu.zjicm.wordsnet_d.util.DataValidator;
import cn.edu.zjicm.wordsnet_d.view.EmailAutoCompleteTextView;

public class ForgetPwdActivity extends WordBaseActivity implements OnClickListener {
	EmailAutoCompleteTextView email;
	ImageView email_clear;
	Button commit;
	ProgressDialog dialog;
	Handler sendHandler = new Handler() {
		public void handleMessage(Message msg) {
			if(msg.what!=ForgetPwdInterface.MSG_START){
				if(dialog!=null){
					dialog.dismiss();
					dialog = null;
				}
			}
			switch (msg.what) {
			case ForgetPwdInterface.MSG_START:
				dialog = ProgressDialog.show(ForgetPwdActivity.this, null, "正在发送邮件...");
				break;
			case ForgetPwdInterface.MSG_SUCCESS:
				Toast.makeText(ForgetPwdActivity.this, "邮件发送成功", Toast.LENGTH_SHORT).show();
				System.out.println("邮件发送成功");
				Intent in = new Intent(ForgetPwdActivity.this, ConfirmCodeActivity.class);
				in.putExtra("email", email.getText().toString());
				startActivity(in);
				break;			
			case ForgetPwdInterface.MSG_CONFIRM_EMAIL:
				Toast.makeText(ForgetPwdActivity.this, "该邮箱尚未注册，不存在", Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_UNSUCCESS:
				Toast.makeText(ForgetPwdActivity.this, "邮件发送失败，请稍后重试", Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_EMAIL_ERROR:
				Toast.makeText(ForgetPwdActivity.this, "请输入正确的电子邮箱", Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_BAD_NETWORK:
				Toast.makeText(ForgetPwdActivity.this, UserInterface.MSG_BAD_NETWORK_STRING, Toast.LENGTH_SHORT).show();
				break;
			case ForgetPwdInterface.MSG_WEB_SITE_ACCESS_FAILURE:
				Toast.makeText(ForgetPwdActivity.this, UserInterface.MSG_WEB_SITE_ACCESS_FAILURE_STRING, Toast.LENGTH_SHORT).show();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("忘记密码");
		setContentView(R.layout.activity_forgetpwd);
		email = (EmailAutoCompleteTextView) findViewById(R.id.forget_email);
		//email.setText("1085124431@qq.com");
		email_clear = (ImageView) findViewById(R.id.forget_email_clear);
		commit = (Button) findViewById(R.id.forget_btn);
		email.addTextChangedListener(textWatcher);
		email_clear.setOnClickListener(this);
		commit.setOnClickListener(this);
	}

	TextWatcher textWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			if (email.getText().length() > 0) {
				commit.setEnabled(true);
				email_clear.setVisibility(View.VISIBLE);
			} else {
				commit.setEnabled(false);
				email_clear.setVisibility(View.GONE);
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			// TODO Auto-generated method stub

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.forget_email_clear:
			email.setText("");
			email_clear.setVisibility(View.GONE);
			break;

		case R.id.forget_btn:
			if(!DataValidator.EmailIsValidate(email.getText().toString())){
				sendHandler.sendMessage(sendHandler.obtainMessage(ForgetPwdInterface.MSG_EMAIL_ERROR));
			}else{
				sendEmail();
			}
			break;
		}
	}

	private void sendEmail() {
		// TODO Auto-generated method stub
		sendHandler.sendMessage(sendHandler.obtainMessage(ForgetPwdInterface.MSG_START));
		ForgetPwdInterface.getInstance(ForgetPwdActivity.this).toSendEmail(email.getText().toString(), sendHandler);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
